/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PanelAlterarSenha.java
 *
 * Created on 22/09/2011, 12:41:40
 */

package modules.estoque.gui.jpanel;

import java.util.logging.Level;
import java.util.logging.Logger;
import modules.estoque.exception.UnexpectedException;
import modules.estoque.facade.WMSFacade;
import modules.estoque.gui.Aplicativo;
import modules.estoque.models.dto.Usuario;
import modules.estoque.security.Codec;
import modules.estoque.security.WMSSession;
import modules.estoque.util.BoxPanel;

/**
 *
 * @author Clezio
 */
public class PanelAlterarSenha extends javax.swing.JPanel {

    private WMSFacade wmsFacade;
    /** Creates new form PanelAlterarSenha */
    public PanelAlterarSenha() {
        initComponents();
        wmsFacade = new WMSFacade();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        lbCubagem = new modules.estoque.gui.custom.WMSLabel();
        btnSalvar = new modules.estoque.gui.custom.WMSJButton();
        lbCubagem1 = new modules.estoque.gui.custom.WMSLabel();
        lbCubagem2 = new modules.estoque.gui.custom.WMSLabel();
        tfSenhaAtual = new javax.swing.JPasswordField();
        tfSenhaNova = new javax.swing.JPasswordField();
        tfSenhaNovaConfirmar = new javax.swing.JPasswordField();

        setBackground(new java.awt.Color(238, 238, 238));

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Alteração de Senha");

        lbCubagem.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lbCubagem.setText("Senha atual:");

        btnSalvar.setText("Salvar");
        btnSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalvarActionPerformed(evt);
            }
        });

        lbCubagem1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lbCubagem1.setText("Nova senha:");

        lbCubagem2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lbCubagem2.setText("Confirmar nova senha:");

        tfSenhaAtual.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tfSenhaAtualKeyPressed(evt);
            }
        });

        tfSenhaNova.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tfSenhaNovaKeyPressed(evt);
            }
        });

        tfSenhaNovaConfirmar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tfSenhaNovaConfirmarKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lbCubagem2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lbCubagem, javax.swing.GroupLayout.DEFAULT_SIZE, 110, Short.MAX_VALUE)
                    .addComponent(lbCubagem1, javax.swing.GroupLayout.DEFAULT_SIZE, 110, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(btnSalvar, javax.swing.GroupLayout.DEFAULT_SIZE, 85, Short.MAX_VALUE)
                        .addComponent(tfSenhaAtual, javax.swing.GroupLayout.DEFAULT_SIZE, 85, Short.MAX_VALUE)
                        .addComponent(tfSenhaNova, javax.swing.GroupLayout.DEFAULT_SIZE, 85, Short.MAX_VALUE))
                    .addComponent(tfSenhaNovaConfirmar, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(582, 582, 582))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 767, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(23, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(88, 88, 88)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbCubagem, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfSenhaAtual, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(9, 9, 9)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbCubagem1, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfSenhaNova, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbCubagem2, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfSenhaNovaConfirmar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(btnSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(328, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalvarActionPerformed
        save();
}//GEN-LAST:event_btnSalvarActionPerformed

    private void tfSenhaAtualKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfSenhaAtualKeyPressed
        if (evt.getKeyCode() == evt.VK_ENTER) {
            tfSenhaNova.requestFocus();
        } else if (evt.getKeyCode() == evt.VK_ESCAPE) {
            tfSenhaAtual.setText("");
        }
    }//GEN-LAST:event_tfSenhaAtualKeyPressed

    private void tfSenhaNovaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfSenhaNovaKeyPressed
        if (evt.getKeyCode() == evt.VK_ENTER) {
            tfSenhaNovaConfirmar.requestFocus();
        } else if (evt.getKeyCode() == evt.VK_ESCAPE) {
            tfSenhaNova.setText("");
            tfSenhaAtual.requestFocus();
        }
    }//GEN-LAST:event_tfSenhaNovaKeyPressed

    private void tfSenhaNovaConfirmarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfSenhaNovaConfirmarKeyPressed
        if (evt.getKeyCode() == evt.VK_ENTER) {
            save();
        } else if (evt.getKeyCode() == evt.VK_ESCAPE) {
            tfSenhaNovaConfirmar.setText("");
            tfSenhaNova.requestFocus();
        }
    }//GEN-LAST:event_tfSenhaNovaConfirmarKeyPressed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private modules.estoque.gui.custom.WMSJButton btnSalvar;
    private javax.swing.JLabel jLabel1;
    private modules.estoque.gui.custom.WMSLabel lbCubagem;
    private modules.estoque.gui.custom.WMSLabel lbCubagem1;
    private modules.estoque.gui.custom.WMSLabel lbCubagem2;
    private javax.swing.JPasswordField tfSenhaAtual;
    private javax.swing.JPasswordField tfSenhaNova;
    private javax.swing.JPasswordField tfSenhaNovaConfirmar;
    // End of variables declaration//GEN-END:variables

    private void save() {
        if (!tfSenhaNova.getText().equals(tfSenhaNovaConfirmar.getText())) {
            BoxPanel.showMessageDialog(null, "O valor digitado no campo senha é diferente do confirmar senha.");
            return;
        }
        Usuario usuarioSession = WMSSession.getInstance().getUser();
        try {
            if (!Codec.encodeBASE64(tfSenhaAtual.getText()).equals(usuarioSession.getSenha())) {
                BoxPanel.showMessageDialog(null, "Senha atual incorreta");
                return;
            }
            usuarioSession.setSenha(Codec.encodeBASE64(tfSenhaNova.getText()));
        } catch (UnexpectedException ex) {
            Logger.getLogger(PanelAlterarSenha.class.getName()).log(Level.SEVERE, null, ex);
            return;
        }

        if (wmsFacade.alterarSenha(usuarioSession)) {
            clearFields();
            BoxPanel.showMessageDialog(null, "Senha alterada com sucesso.");
            WMSSession.getInstance().getTelaLogin().show();
            WMSSession.getInstance().getAplicativo().dispose();
        }
    }

    private void clearFields() {
        tfSenhaAtual.setText("");
        tfSenhaNova.setText("");
        tfSenhaNovaConfirmar.setText("");
    }

}
